#include "wxsmediactrl.h"

#if wxUSE_MEDIACTRL

#include "wx/mediactrl.h"


namespace {
  wxsRegisterItem<wxsMediaCtrl> Reg( _T( "MediaCtrl" ), wxsTWidget, _T( "Standard" ), 210 );

  WXS_EV_BEGIN( wxsMediaCtrlEvents )
  WXS_EVI( EVT_MEDIA_LOADED, wxEVT_MEDIA_LOADED, wxMediaEvent, Loaded )
  WXS_EVI( EVT_MEDIA_PLAY, wxEVT_MEDIA_PLAY, wxMediaEvent, Play )
  WXS_EVI( EVT_MEDIA_PAUSE, wxEVT_MEDIA_PAUSE, wxMediaEvent, Pause )
  WXS_EVI( EVT_MEDIA_STATECHANGED, wxEVT_MEDIA_STATECHANGED, wxMediaEvent, StateChanged )
  WXS_EVI( EVT_MEDIA_STOP, wxEVT_MEDIA_STOP, wxMediaEvent, Stop )
  WXS_EVI( EVT_MEDIA_FINISHED, wxEVT_MEDIA_FINISHED, wxMediaEvent, Finished )
  WXS_EV_END()
}


wxsMediaCtrl::wxsMediaCtrl( wxsItemResData* Data ):
  wxsWidget(
    Data,
    &Reg.Info,
    wxsMediaCtrlEvents,
    NULL ),
  m_sMediaFile( wxEmptyString ),
  m_sProxy( wxEmptyString ),
  m_iControls( wxMEDIACTRLPLAYERCONTROLS_DEFAULT ),
  m_iVolume( 5 )

{}


void wxsMediaCtrl::OnBuildCreatingCode() {
  switch( GetLanguage() ) {
    case wxsCPP: {
      AddHeader( _T( "<wx/mediactrl.h>" ), GetInfo().ClassName, hfInPCH );
      Codef( _T( "%C(%W, %I, %s, %P, %S, %T);\n" ), wxT( "wxEmptyString" ) );
      if( m_sMediaFile.StartsWith( wxT( "http://" ) ) ) {
        if( !m_sProxy.IsEmpty() ) {
          Codef( _T( "%ALoadURIWithProxy(%n, %n);\n" ), m_sMediaFile.wx_str(), m_sProxy.wx_str() );
        } else
        { Codef( _T( "%ALoadURI(%n);\n" ), m_sMediaFile.wx_str() ); }
      } else
      { Codef( _T( "%ALoad(%n);\n" ), m_sMediaFile.wx_str() ); }
      switch( m_iControls ) {
        case wxMEDIACTRLPLAYERCONTROLS_NONE:
          Codef( _T( "%AShowPlayerControls(%s);\n" ), wxT( "wxMEDIACTRLPLAYERCONTROLS_NONE" ) );
          break;
        case wxMEDIACTRLPLAYERCONTROLS_STEP:
          Codef( _T( "%AShowPlayerControls(%s);\n" ), wxT( "wxMEDIACTRLPLAYERCONTROLS_STEP" ) );
          break;
        case wxMEDIACTRLPLAYERCONTROLS_VOLUME:
          Codef( _T( "%AShowPlayerControls(%s);\n" ), wxT( "wxMEDIACTRLPLAYERCONTROLS_VOLUME" ) );
          break;
        case wxMEDIACTRLPLAYERCONTROLS_DEFAULT:
          Codef( _T( "%AShowPlayerControls(%s);\n" ), wxT( "wxMEDIACTRLPLAYERCONTROLS_DEFAULT" ) );
          break;
      }
      wxString sVol;
      if( m_iVolume == 10 ) {
        sVol = wxT( "1.0" );
      } else
      { sVol = _F( wxT( "0.%ld" ), m_iVolume ); }
      Codef( _T( "%ASetVolume(%s);\n" ), sVol.c_str() );
      BuildSetupWindowCode();
      return;
    }
    default: {
      wxsCodeMarks::Unknown( _T( "wxsMediaCtrl::OnBuildCreatingCode" ), GetLanguage() );
    }
  }
}


wxObject* wxsMediaCtrl::OnBuildPreview( wxWindow* parent, long flags ) {
  wxMediaCtrl* preview = new wxMediaCtrl( parent, GetId(), wxEmptyString, Pos( parent ), Size( parent ), Style() ); //, m_sBackend);
  if( m_sMediaFile.StartsWith( wxT( "http://" ) ) ) {
    if( !m_sProxy.IsEmpty() ) {
      preview->LoadURIWithProxy( m_sMediaFile, m_sProxy );
    } else
    { preview->LoadURI( m_sMediaFile ); }
  } else {
    preview->Load( m_sMediaFile );
  }
  switch( m_iControls ) {
    case wxMEDIACTRLPLAYERCONTROLS_NONE:
      preview->ShowPlayerControls( wxMEDIACTRLPLAYERCONTROLS_NONE );
      break;
    case wxMEDIACTRLPLAYERCONTROLS_STEP:
      preview->ShowPlayerControls( wxMEDIACTRLPLAYERCONTROLS_STEP );
      break;
    case wxMEDIACTRLPLAYERCONTROLS_VOLUME:
      preview->ShowPlayerControls( wxMEDIACTRLPLAYERCONTROLS_VOLUME );
      break;
    case wxMEDIACTRLPLAYERCONTROLS_DEFAULT:
      preview->ShowPlayerControls( wxMEDIACTRLPLAYERCONTROLS_DEFAULT );
      break;
  }
  preview->SetVolume( ( double )m_iVolume / 10 );
  return SetupWindow( preview, flags );
}


void wxsMediaCtrl::OnEnumWidgetProperties( long flags ) {
  static const long VolumeLevels[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  static const wxChar* VolumeNames[]  = {wxT( "0.0" ), wxT( "0.1" ), wxT( "0.2" ), wxT( "0.3" ), wxT( "0.4" ), wxT( "0.5" ), wxT( "0.6" ), wxT( "0.7" ), wxT( "0.8" ), wxT( "0.9" ), wxT( "1.0" ), NULL};
  static const long ControlStates[] = {wxMEDIACTRLPLAYERCONTROLS_NONE,
                                       wxMEDIACTRLPLAYERCONTROLS_STEP,
                                       wxMEDIACTRLPLAYERCONTROLS_VOLUME,
                                       wxMEDIACTRLPLAYERCONTROLS_DEFAULT
                                      };
  static const wxChar* ControlNames[]  = {wxT( "None" ), wxT( "Step" ), wxT( "Volume" ), wxT( "Default" ), NULL};
  WXS_SHORT_STRING( wxsMediaCtrl, m_sMediaFile, _T( "Media File" ), _T( "media_file" ), _T( "" ), false )
  WXS_SHORT_STRING( wxsMediaCtrl, m_sProxy, _T( "Proxy" ), _T( "proxy" ), _T( "" ), false )
  WXS_ENUM( wxsMediaCtrl, m_iControls, _T( "Controls" ), _T( "controls" ), ControlStates, ControlNames, wxMEDIACTRLPLAYERCONTROLS_DEFAULT )
  WXS_ENUM( wxsMediaCtrl, m_iVolume,  _T( "Volume" ), _T( "volume" ), VolumeLevels, VolumeNames, 5 )
}

#endif

